/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.xml;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;
import org.xml.sax.SAXException;

/**
 * Utility class for XML files
 * @author Peter
 */
public class XmlUtils {

    /**
     * Validates an XML source against XSD File(s)
     * @param xsdFileName
     * @param xmlURL
     * @return true if valid
     */
    public static boolean validateXML(String xsdFileName, URL xmlURL) {
        String[] xsdFileNames = {xsdFileName};
        return validateXML(xsdFileNames, xmlURL);
    }

    /**
     * Validates an XML source against XSD File(s)
     * @param xsdFileNames
     * @param xmlURL
     * @return true if valid
     */
    public static boolean validateXML(String[] xsdFileNames, URL xmlURL) {
        int length = xsdFileNames.length;
        Source xsdSource[] = new StreamSource[length];

        for (int i = 0; i < length; i++) {
            xsdSource[i] = new StreamSource(xsdFileNames[i]);
        }
        try {
            Source xmlSource = new StreamSource(xmlURL.openStream());
            return validateXML(xsdSource, xmlSource);
        } catch (IOException ex) {
            System.out.println("could not open" + xmlURL.toString());
            System.out.println(ex.getMessage());
            return false;
        }

    }

    /**
     * Validates an XML source against XSD File(s)
     * @param xsdSource
     * @param xmlFileName
     * @return true if valid
     */
    public static boolean validateXML(String xsdSource, String xmlFileName) {
        String xsdSources[] = new String[1];
        xsdSources[0] = xsdSource;
        return validateXML(xsdSources, xmlFileName);
    }

    /**
     * Validates an XML source against XSD File(s)
     * @param xsdSources
     * @param xmlFileName
     * @return true if valid
     */
    public static boolean validateXML(String xsdSources[], String xmlFileName) {
        int noXsdSourses = xsdSources.length;
        Source[] fileSources = new Source[noXsdSourses];
        for (int i = 0; i < noXsdSourses; i++) {
            fileSources[i] = new StreamSource(new File(xsdSources[i]));
        }
        Source xmlSource = new StreamSource(xmlFileName);

        return validateXML(fileSources, xmlSource);

    }

    /**
     * Validates an XML source against XSD File(s)
     * @param xsdSources 
     * @param xmlSource 
     * @return true if valid
     */
    public static boolean validateXML(Source[] xsdSources, Source xmlSource) {
        // create schema from XSD
        SchemaFactory factory =
                SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        Schema schema;
        try {
            schema = factory.newSchema(xsdSources);
        } catch (SAXException ex) {
            System.out.println(ex.getMessage());
            return false;
        }

        // get the validator
        Validator validator = schema.newValidator();

        // validate the XML using the validator
        try {
            validator.validate(xmlSource);
        } catch (SAXException ex) {
            System.out.println(xmlSource + " is not valid because ");
            System.out.println(ex.getMessage());
            return false;
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
            return false;
        }
        return true;

    }

    /**
     * Validates an XML source against XSD File(s)
     * @param xsdSource
     * @param xmlUrl
     * @return
     */
    static boolean validateXML(Source xsdSource, URL xmlUrl) {
        Source[] xsdSources = {xsdSource};
        
        try {
            Source xmlSource = new StreamSource(xmlUrl.openStream());
            return validateXML(xsdSources, xmlSource);
        } catch (IOException ex) {
            System.out.println("could not open" + xmlUrl.toString());
            System.out.println(ex.getMessage());
            return false;
        }
    }
    /**
     * Validates an XML source against XSD File(s)
     * @param xsdSource
     * @param xmlUrl
     * @return
     */
    static boolean validateXML(Source[] xsdSource, URL xmlUrl) {
        try {
            Source xmlSource = new StreamSource(xmlUrl.openStream());
            return validateXML(xsdSource, xmlSource);
        } catch (IOException ex) {
            System.out.println("could not open" + xmlUrl.toString());
            System.out.println(ex.getMessage());
            return false;
        }
    }
}
